<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use App\Models\Traits\GameTypeTrait;

class HomeController extends Controller
{
    use GameTypeTrait;

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('控制面板');
            $content->description('Description...');

            $content->row('基本统计信息');

            $content->row(function (Row $row) {

                $row->column(6, function (Column $column) {
                    $name = 'typhoon';
                    $title ='台风题库正确率统计';
                    $game = $this->game_types[$name];
                    $correct_count = $this->calculateList($game);
                    $sum = 100;

                    $column->append(view('admin.charts.doughnut',compact(['correct_count', 'sum', 'name', 'title'])));
                });

                $row->column(6, function (Column $column) {
                    $title = '地震题库正确率统计';
                    $name = 'earthquake';
                    $game = $this->game_types[$name];
                    $correct_count = $this->calculateList($game);
                    $sum = 100;
                    $column->append(view('admin.charts.doughnut',compact(['correct_count', 'sum', 'name', 'title'])));
                });
            });
            $content->row(function (Row $row) {

                $row->column(6, function (Column $column) {
                    $title = '八礼题库正确率统计';
                    $name = 'etiquette';
                    $game = $this->game_types[$name];
                    $correct_count = $this->calculateList($game);
                    $sum = 100;
                    $column->append(view('admin.charts.doughnut',compact(['correct_count', 'sum', 'name', 'title'])));
                });

                $row->column(6, function (Column $column) {
                    $title = '火灾题库正确率统计';
                    $name = 'fire';
                    $game = $this->game_types[$name];
                    $correct_count = $this->calculateList($game);
                    $sum = 100;
                    $column->append(view('admin.charts.doughnut',compact(['correct_count', 'sum', 'name', 'title'])));
                });
            });
        });
    }

    private function calculateList($game)
    {
        $questions = $game::all();
        $data = 0;
        if($questions){
            foreach ($questions as $question)
            {
                $data += $this->calculateItem($question);
            }
            return round($data / count($questions));
        } else {
         return 0;
        }
    }

    private function calculateItem($item)
    {
        $counter = $item->counter;
        if(!$counter){
            return 0;
        }
        $decode_counter = json_decode($counter, true);
        $sum = 0;
        foreach ($decode_counter as $value)
        {
            $sum += $value;
        }
        $correct_count = $item->correct_count;
        return round($correct_count / $sum * 100);
    }
}
